package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.SysRoler;
import com.liu.manager.mapper.SysrolerMapper;
import com.liu.manager.service.SysrolerService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysrolerSvcImpl implements SysrolerService {

    @Resource
    private SysrolerMapper sysrolerMapper;

    @Override
    public ResponseResult insertRoler(SysRoler sysRoler) {
        if(!StringUtils.hasLength(sysRoler.getRoleName())){
            return ResponseResult.ERROR("角色名称为空");
        }
        if(sysRoler.getCreateTime() == 0){
            sysRoler.setCreateTime(System.currentTimeMillis());
            sysRoler.setUpdateTime(System.currentTimeMillis());
        }
        sysRoler.setStatus(1);
        int result = sysrolerMapper.insertRoler(sysRoler);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(sysRoler);
        return responseResult;
    }

    @Override
    public ResponseResult deleteRoler(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        int row = sysrolerMapper.deleteRoler(id);
        if(row > 0){
            return ResponseResult.OK("删除成功");
        }
        return ResponseResult.ERROR("删除失败");
    }

    @Override
    public ResponseResult getAllrolers() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(sysrolerMapper.getSysrolers());
        return responseResult;
    }

    @Override
    public ResponseResult findSysRolerById(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(sysrolerMapper.findById(id));
        return responseResult;
    }
}
